import { Application } from 'egg';
import * as moment from 'moment';
import * as pageFindPlugin from 'mongoose-paginate';
import { enjoyOrderStatus } from '../constants/index';
const statusEnum = enjoyOrderStatus.map(item => item.key);
export default (app: Application) => {
  const mongoose = app.mongoose;
  const ObjectId = mongoose.Schema.Types.ObjectId;
  const schema = new mongoose.Schema({
    // 订单号
    orderNo: {
      type: Number,
      required: true,
    },
    // 用户
    user: {
      type: ObjectId,
      required: true,
      ref: 'User',
    },
    // 联系人姓名
    customName: {
      type: String,
      required: true,
    },
    // 联系人电话
    customPhone: {
      type: String,
      required: false,
    },
    // 门店
    store: {
      type: ObjectId,
      required: true,
      ref: 'Site',
    },
    // 共计费用
    totalFee: {
      type: Number,
      required: true,
    },
    // 实付费用
    actuallyFee: {
      type: Number,
      required: true,
    },
    // 数量
    amount: {
      type: Number,
      required: true,
      default: 1,
    },
    // 物品
    goods: {
      type: ObjectId,
      required: true,
      ref: 'Enjoy',
    },
    // sku
    sku: {
      type: ObjectId,
      required: true,
      ref: 'Sku',
    },
    // 状态
    status: {
      type: String,
      required: true,
      enum: statusEnum,
      default: enjoyOrderStatus[0].key,
    },
    // 预约码信息
    promotion: {
      code: String,
      appointmentUrl: String,
    },
    // 操作记录
    logs: [{
      user: {
        id: ObjectId,
        name: String,
      },
      action: String,
      ctime: String,
    }],
  }, {
      toJSON: { getters: true },
      timestamps: true,
    });
  // tslint:disable-next-line:ter-prefer-arrow-callback
  schema.virtual('ctime').get(function (this) {
    return moment(this.createdAt).format('YYYY/MM/DD HH:mm');
  });
  schema.plugin(pageFindPlugin);
  return mongoose.model('Enjoyorder', schema, 'enjoyorder');
};
